<?php 
error_reporting(E_ALL);
ini_set("display_errors", "On");
header('Content-Type: text/html; charset=UTF-8');
require 'class_mysql.php';
$c['host'] = '127.0.0.1';
$c['username'] = 'root';
$c['password'] = '';
$c['dbname'] = 'lvzhiwo';
$c['charset'] = 'utf8'; 
$db = new Mysql($c);


function get_cache($name){
	global $db;
	$res = $db->get("SELECT * FROM `cache` WHERE `name` = '$name' ORDER BY  `id` DESC ");
	return 	isset($res->value)?$res->value:null;
}
function set_cache($name, $value){
	global $db;
	$row = array('name'=>$name, 'value'=>addslashes($value));
	return $db->save('cache', $row);
	
}



$proxy = 'web-proxy.oa.com:8080';
//$proxy = '';
function get_url($url, $proxy='' ,$update_cache = false){

	$flag_write_cache = false;
	$cache_name = $url;
	if($update_cache == false){
		$cache = get_cache($cache_name);
		if($cache===null){		
			$flag_write_cache = true;
		}else{
			return $cache;
		};
	}else{
		$flag_write_cache = true;
	}
	$cookie_file = 'cookie.txt';
	$http_code = 302;
	$i = 1; 
	$old_url = $url = $url;
	// create a new cURL resource
	while($http_code=='302' and $i < 6){
		$ch = curl_init();
		// set URL and other appropriate options
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt ($ch, CURLOPT_REFERER, $old_url);
		if($proxy!=''){
			curl_setopt ($ch, CURLOPT_PROXY, "web-proxy.oa.com:8080");
		}
		curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt ($ch, CURLOPT_PROXYUSERPWD, '');
		$result = curl_exec($ch);
		if ($result!==false) {
			$info_array = curl_getinfo($ch);
			//判断http_code
			$http_code = $info_array['http_code'];
			$old_url = $url;
			if(isset($info_array['redirect_url'])){
				$url =  $info_array['redirect_url'];
			}else{
				list($header) = explode("\r\n\r\n", $result, 2);
				$matches = array();
				preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
				$url = trim(array_pop($matches));
				$url_parsed = parse_url($url);
				if (isset($url_parsed)) {
					$url = $url;
				}
				
			}
			
		}else{
			die('返回错误:' . curl_error($ch));
		}
		curl_close($ch);
		$i++;
	}
	if($i==6){
		die('获取错误'.$result) ;	
	}
	$result = mb_convert_encoding($result, 'UTF-8','GBK');
	if($flag_write_cache===true){
		set_cache($cache_name, $result);
	}
	return $result;

}

$proxy = 'web-proxy.oa.com:8080';
if(isset($_GET['id'])){
	$id= ($_GET['id']);
}else{
	die();
}

$res = $db->get("SELECT `goods_id`,`goods_sn` FROM `ecs_goods` WHERE `goods_id` = $id");

if (isset($res->goods_sn)){
	$item_add = 'http://detail.tmall.com/item.htm?id='.$res->goods_sn;
	
	$res_obj = $db->get("SELECT * FROM `cache` WHERE `name` like '{$item_add}%'");
	if(isset($res_obj->value)){
		$content = $res_obj->value;
	}else{
		$content = get_url($item_add.'&' ,$proxy);
	}
	$m = preg_match('/data-reviewApi="(.*?)"/is', $content, $matches);

	//如果没获取到则更新网页缓存
	if(count($matches)==0){
		$content = get_url($item_add.'&' ,$proxy ,true);
		$m = preg_match('/data-reviewApi="(.*?)"/is', $content, $matches);
	}
	if(count($matches)==0){
		die($item_add.'&没有匹配到reviewApi');
	}
	$content = get_url($matches[1] ,$proxy);
	$content = strstr($content,'{');
	$content_array = json_decode($content, true);
	$content_array = $content_array['rateListInfo'];
	if(count($content_array)>0){
		foreach ($content_array['rateList'] as $key => $value){
			$time = strtotime(trim($value['rateDate']));
			if($time==''){
				$time = strtotime('2012-'.rand(1,5).'-'.rand(1,30).' '.rand(1,24).':'.rand(1,60).':'.rand(1,60));
			}
			$row = array(
					'comment_type'=>1, 
					'id_value'=>$id,
					'email'=>rand(10000,9999999).'@qq.com',
					'user_name'=>$value['displayUserNick'],
					'content'=>'<span id="auctionSku">'.$value['auctionSku'].'</span>'.$value['rateContent'],
					'comment_rank'=>rand(3,5),
					'add_time'=>$time,
					'ip_address'=>'207.10.108.10',
					'status'=>1,
					'user_id'=>88,
					);
			$db->save('ecs_comment', $row);
		}
	}
	$id = $id+1; 
	echo "<script language=javascript>
setTimeout(\"window.location.href='evaluate.php?id=$id'\",2000)
</script>";
	$content = strstr($content,'{');
	//print_r($content);
	//print_r(json_decode($content));
}else{
	echo $id.'success';
}


//图片 后面 一个个更新
function save_image($url){
	return $url;
}



die();



























function del_desc(){
	die();
	//去掉海岩产品描述中的 促销信息
	$res_array = $db->find("SELECT `goods_id`,`goods_desc` FROM `ecs_goods` WHERE `goods_id` > 75");

	foreach ($res_array as $key => $res){

		$des = preg_replace('/<td[^>]*>(.*?)<\/td>/is', '', $res->goods_desc);

		$des = preg_replace('/<tr[^>]*>(.*?)<\/tr>/is', '', $des);
		$des = preg_replace('/<a[^>]*><\/a>/is', '', $des);
		$des = preg_replace('/<table[^>]*>(.*?)<\/table>/is', '', $des);
		$des = str_replace('</td>', '', $des);
		$des = str_replace('</tr>', '', $des);
		$des = str_replace('</table>', '', $des);
		$des = str_replace('<p></p>', '', $des);
		$des = str_replace('http://img04.taobaocdn.com/imgextra/i4/392195157/T2qQeIXgdaXXXXXXXX_!!392195157.jpg', '', $des);
		$des = str_replace('http://img02.taobaocdn.com/imgextra/i2/392195157/T2QzmGXXJaXXXXXXXX_!!392195157.gif', '', $des);
		$des = str_replace('http://img03.taobaocdn.com/imgextra/i3/392195157/T2BkCIXbFXXXXXXXXX_!!392195157.jpg', '', $des);
		$des = str_replace('http://img03.taobaocdn.com/imgextra/i3/392195157/T2HByIXe0XXXXXXXXX_!!392195157.jpg', '', $des);

		$des = addslashes($des);

		if($des!=''){
			echo $db->query("update `ecs_goods` set `goods_desc` = '{$des}' where goods_id = {$res->goods_id}");
		}

	}
}



